package com.lfd.base64jwt;

import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import io.jsonwebtoken.security.Keys;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import javax.crypto.spec.SecretKeySpec;
import java.security.Key;
import java.util.Base64;
import java.util.Date;

@Slf4j
@SpringBootApplication
public class Base64JwtApplication implements ApplicationRunner {
    @Override
    public void run(ApplicationArguments args) throws Exception {
        testBase64();
        testJwt();
        testJwt2();
    }

    private void testBase64() {
        // 原始字符串
        String originalString = "Hello, Base64!";
        // 编码
        String encodedString = Base64.getEncoder().encodeToString(originalString.getBytes());
        log.info("testBase64 编码{}", encodedString);
        // 解码
        byte[] decode = Base64.getDecoder().decode(encodedString);
        String decodedString = new String(decode);
        log.info("testBase64 解码{}", decodedString);
    }

    private void testJwt() {
        // 生成密钥
        Key key = Keys.secretKeyFor(SignatureAlgorithm.HS256);

        // 生成 JWT
        String jwt = Jwts.builder()
                .setSubject("user1")
                .setIssuer("myApp")
                .setIssuedAt(new Date())
                .setExpiration(new Date(System.currentTimeMillis() + 3600000)) // 1 小时过期
                .signWith(key)
                .compact();

        System.out.println("Generated JWT: " + jwt);
        // 解析 JWT
        String subject = Jwts.parser()
                .setSigningKey(key)
                .build()
                .parseClaimsJws(jwt)
                .getBody()
                .getSubject();

        System.out.println("Decoded Subject: " + subject);
    }
    public static void testJwt2() {
        // 自定义的字符串秘钥（在生产环境中，确保这个秘钥足够复杂和安全）
        String secretString = "myCustomSecretKeyForJwtSigning1234567890";

        // 将字符串转换为字节数组并生成 SecretKey
        byte[] secretKeyBytes = Base64.getDecoder().decode(Base64.getEncoder().encode(secretString.getBytes()));
        Key secretKey = new SecretKeySpec(secretKeyBytes, SignatureAlgorithm.HS256.getJcaName());

        // 生成 JWT
        String jwt = Jwts.builder()
                .setSubject("user123")
                .setIssuer("myApp")
                .setIssuedAt(new Date())
                .setExpiration(new Date(System.currentTimeMillis() + 3600000)) // 1 小时过期
                .signWith(secretKey, SignatureAlgorithm.HS256)
                .compact();

        System.out.println("Generated JWT: " + jwt);

        // 解析 JWT
        String subject = Jwts.parser()
                .setSigningKey(secretKey)
                .build()
                .parseClaimsJws(jwt)
                .getBody()
                .getSubject();

        System.out.println("Decoded Subject: " + subject);
    }
    public static void main(String[] args) {
        SpringApplication.run(Base64JwtApplication.class, args);
    }

}
